<?php

require_once dirname(__FILE__) . '/../lib/BaselzprofileActions.class.php';

/**
 * lzprofile actions.
 * 
 * @package    sfLoginzaPropelPlugin
 * @subpackage lzprofile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
class lzprofileActions extends BaselzprofileActions {

    public function executeIndex(sfWebRequest $request) {

        $this->auth_user = $this->getUser();
        $this->user = LzUserPeer::retrieveByPK($this->auth_user->getAttribute('id'));

        if ($this->auth_user->hasAttribute('first_name')) {
            $this->full_name = $this->auth_user->getAttribute('first_name') . ' ' . $this->auth_user->getAttribute('last_name');
        } else if ($this->auth_user->hasAttribute('nickname')) {
            $this->full_name = $this->auth_user->getAttribute('nickname');
        } else if ($this->auth_user->hasAttribute('email')) {
            $this->full_name = $this->auth_user->getAttribute('email');
        } else {
            $this->full_name = $this->auth_user->getAttribute('identity');
        }

        $this->error_not_found = null;

        if (!$this->user)
            $this->error_not_found = 'User not found';
        else {
            /**
             * @var UserProvider $this->current_provider
             */
            $this->current_provider = $this->user->getLzUserProvider();
        }
    }

    public function executeOverProviders(sfWebRequest $request) {

        $this->auth_user = $this->getUser();
        $this->user = LzUserPeer::retrieveByPK($this->auth_user->getAttribute('id'));
        $this->full_name = $this->auth_user->getAttribute('first_name') . ' '
                . $this->auth_user->getAttribute('last_name');

        $this->error_not_found = null;
        $this->error = null;

        if (!$this->user)
            $this->error_not_found = 'User not found';
        else {
            // if HTTP-Method == POST, then create new user_provider for current user
            if ($request->getMethod() == 'POST') {
                $url = sfConfig::get('app_LoginzaPlugin_loginza_auth_info') . '?token=' . $request->getParameter('token');
                $this->loginza_response = json_decode(file_get_contents($url), true);

                if (isset($this->loginza_response['error_type'])) {
                    $this->error = 'Authtorization error!';
                } else {
                    // get such user_provider from db
                    $criteria = new Criteria();
                    $criteria->add(LzUserProviderPeer::IDENTITY, $this->loginza_response['identity'], Criteria::EQUAL);
                    $user_provider = LzUserProviderPeer::doSelectOne($criteria);

                    //if this user already exist in your or in other profile, then ERROR
                    if ($user_provider) {
                        $criteria = new Criteria();
                        $criteria->add(LzInternalExternalIdsPeer::ID_EXTERNAL, $user_provider->getId(), Criteria::EQUAL);
                        $in_ex = LzInternalExternalIdsPeer::doSelectOne($criteria);
                        if ($in_ex->getIdInternal() == $this->user->getId())
                            $this->error = 'this provider already exists in your profile!';
                        else
                            $this->error = 'this provider already exists in anothers profile!';
                    }else {
                        //add new user_provider for your profile
                        $id_new_user_provider = $this->createNewUserProvider();

                        $in_ex = new LzInternalExternalIds();
                        $in_ex->setIdInternal($this->user->getId());
                        $in_ex->setIdExternal($id_new_user_provider);
                        $in_ex->save();
                    }
                }

                if (!$this->error) //if hasn't any error
                    return $this->redirect('@overproviders');
            } else {
                // no POST, output providers of current user
                $in_ex_array = $this->user->getLzInternalExternalIdss();
                $this->my_providers = array();

                $id_user_provider_user = $this->user->getIdUserProvider();
                $this->my_current_provider = $this->user->getLzUserProvider();

                foreach ($in_ex_array as $item) {
                    $up = $item->getLzUserProvider();
                    if ($up->getId() != $id_user_provider_user)
                        $this->my_providers[] = $up;
                }
            }
        }
    }

    public function executeRemoveProvider(sfWebRequest $request) {
        $provider = LzUserProviderPeer::retrieveByPK($request->getParameter('id'));
        if ($provider) {
            $criteria = new Criteria();
            $criteria->add(LzInternalExternalIdsPeer::ID_EXTERNAL, $provider->getId(), Criteria::EQUAL);
            $in_ex = LzInternalExternalIdsPeer::doSelectOne($criteria);
            $in_ex->delete();
            $data = $provider->delete();
            return true;
        }
        return 'provider is not found';
    }

    protected function createNewUserProvider() {
        $new_user_provider = new LzUserProvider();

        $new_user_provider->setIdentity($this->loginza_response['identity']);
        $new_user_provider->setProvider($this->loginza_response['provider']);
        if (isset($this->loginza_response['name'])) {
            $new_user_provider->setFirstName($this->loginza_response['name']['first_name']);
            $new_user_provider->setLastName($this->loginza_response['name']['last_name']);
        }
        if (isset($this->loginza_response['uid']))
            $new_user_provider->setUid($this->loginza_response['uid']);
        if (isset($this->loginza_response['nickname']))
            $new_user_provider->setNick($this->loginza_response['nickname']);
        if (isset($this->loginza_response['email'])) {
            $new_user_provider->setEmail($this->loginza_response['email']);
        }
        if (isset($this->loginza_response['gender']))
            $new_user_provider->setGender($this->loginza_response['gender']);
        if (isset($this->loginza_response['dob']))
            $new_user_provider->setBirthday($this->loginza_response['dob']);
        if (isset($this->loginza_response['im'])) {
            if (isset($this->loginza_response['im']['icq']))
                $new_user_provider->setICQ($this->loginza_response['im']['icq']);
            if (isset($this->loginza_response['im']['skype']))
                $new_user_provider->setSkype($this->loginza_response['im']['skype']);
        }
        if (isset($this->loginza_response['biography']))
            $new_user_provider->setBiography($this->loginza_response['biography']);
        if (isset($this->loginza_response['photo'])) {
            $new_user_provider->setPhoto($this->loginza_response['photo']);
        }
        if (isset($this->loginza_response['work'])) {
            if (isset($this->loginza_response['work']['company']))
                $new_user_provider->setCompany($this->loginza_response['work']['company']);
            if (isset($this->loginza_response['work']['job']))
                $new_user_provider->setJob($this->loginza_response['work']['job']);
        }
        if (isset($this->loginza_response['adress'])) {
            if (isset($this->loginza_response['adress']['home']))
                $new_user_provider->setHomeAdress($this->loginza_response['adress']['home']);
            if (isset($this->loginza_response['adress']['business']))
                $new_user_provider->setBusinessAdress($this->loginza_response['adress']['business']);
        }
        if (isset($this->loginza_response['phone'])) {
            if (isset($this->loginza_response['phone']['preferred']))
                $new_user_provider->setPreferredPhone($this->loginza_response['phone']['preferred']);
            if (isset($this->loginza_response['phone']['home']))
                $new_user_provider->setHomePhone($this->loginza_response['phone']['home']);
            if (isset($this->loginza_response['phone']['work']))
                $new_user_provider->setWorkPhone($this->loginza_response['phone']['work']);
            if (isset($this->loginza_response['phone']['mobile']))
                $new_user_provider->setMobilePhone($this->loginza_response['phone']['mobile']);
            if (isset($this->loginza_response['phone']['fax']))
                $new_user_provider->setFax($this->loginza_response['phone']['fax']);
        }
        if (isset($this->loginza_response['web'])) {
            if (isset($this->loginza_response['web']['default']))
                $new_user_provider->setWeb($this->loginza_response['web']['default']);
            if (isset($this->loginza_response['web']['blog']))
                $new_user_provider->setBlog($this->loginza_response['web']['blog']);
        }

        $new_user_provider->save();

        return $new_user_provider->getId();
    }

}
